package newsSystem.Admin;

import newsSystem.user.UserInfo;


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import static newsSystem.dao.UserInfoJDBC.addUserByUserInfo;

public class AddUserWindow extends JFrame {

    private JTextField userIDField, userPWDField, userNameField, idCardField, phoneField, addressField;
    private JComboBox<String> userSexComboBox;
    private Runnable refreshTableCallback;

    public AddUserWindow(Runnable refreshTableCallback) {
        this.refreshTableCallback = refreshTableCallback;
        setTitle("添加用户");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(300, 350);
        setLocationRelativeTo(null);

        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 1);
        gbc.anchor = GridBagConstraints.WEST;

        // UserID
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(new JLabel("UserID:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        userIDField = new JTextField(15);
//        userIDField.setFont(fieldFont);
        userIDField.setFont(new Font(Font.SERIF, Font.PLAIN, 12)); // 使用系统默认的衬线字体，通常支持中文
        userIDField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(userIDField, gbc);


        // UserPWD
        gbc.gridx = 0;
        gbc.gridy = 1;
        mainPanel.add(new JLabel("UserPWD:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        userPWDField = new JTextField(15);
//        userPWDField.setFont(fieldFont);
        userPWDField.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        userPWDField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(userPWDField, gbc);

        // UserName
        gbc.gridx = 0;
        gbc.gridy = 2;
        mainPanel.add(new JLabel("UserName:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        userNameField = new JTextField(15);
//        userNameField.setFont(fieldFont);
        userNameField.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        userNameField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(userNameField, gbc);

        // UserSex
        gbc.gridx = 0;
        gbc.gridy = 3;
        mainPanel.add(new JLabel("UserSex:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        // 创建性别下拉框，并添加选项
        String[] genders = {"男", "女"}; // 可以根据需要添加更多选项
        JComboBox<String> userSexComboBox = new JComboBox<>(genders);

// 设置下拉框的字体和尺寸
        userSexComboBox.setPreferredSize(new Dimension(150, 27)); // 设置下拉框的宽度和高度
// 设置下拉框的字体为系统默认字体
        userSexComboBox.setFont(UIManager.getDefaults().getFont("ComboBox.font"));
        mainPanel.add(userSexComboBox, gbc);

        // IDCard
        gbc.gridx = 0;
        gbc.gridy = 4;
        mainPanel.add(new JLabel("IDCard:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        idCardField = new JTextField(15);
//        idCardField.setFont(fieldFont);
        idCardField.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        idCardField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(idCardField, gbc);

        // Phone
        gbc.gridx = 0;
        gbc.gridy = 5;
        mainPanel.add(new JLabel("Phone:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        phoneField = new JTextField(15);
//        phoneField.setFont(fieldFont);
        phoneField.setFont(new Font(Font.SERIF, Font.PLAIN, 12)); // 使用系统默认的衬线字体，通常支持中文
        phoneField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(phoneField, gbc);

        // Address
        gbc.gridx = 0;
        gbc.gridy = 6;
        mainPanel.add(new JLabel("Address:"), gbc);

        gbc.gridx = 1;
        gbc.gridy = 6;
        addressField = new JTextField(15);
//        addressField.setFont(fieldFont);
        addressField.setFont(new Font(Font.SERIF, Font.PLAIN, 12));
        addressField.setPreferredSize(new Dimension(150, 27)); // 设置文本框的宽度和高度
        mainPanel.add(addressField, gbc);


//        // 创建确定按钮
//        JButton submitButton = new JButton("确定");
//        gbc.gridx = 0;
//        gbc.gridy = 7;
//        gbc.gridwidth = 2; // 占据两列
//        mainPanel.add(submitButton, gbc);

        // 提交按钮
        gbc.gridx = 0;
        gbc.gridy = 9;
        gbc.gridwidth = 1; // 设置横跨列数为1
        gbc.anchor = GridBagConstraints.CENTER; // 组件居中对齐
        JButton submitButton = new JButton("添加");
        mainPanel.add(submitButton, gbc);

// 重置按钮
        gbc.gridx = 1; // 将gridx的值更新为1，使重置按钮放置在下一列
        gbc.gridy = 9; // 将gridy的值保持为8，保持与提交按钮在同一行
        JButton cancelButton = new JButton("取消");
        mainPanel.add(cancelButton, gbc);



        // 添加确定按钮的动作监听器
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 获取文本框和下拉框中的数据
                String userId = userIDField.getText();
                String userPwd = userPWDField.getText();
                String userName = userNameField.getText();
                String gender = (String) userSexComboBox.getSelectedItem();
                String idCard = idCardField.getText();
                String phone = phoneField.getText();
                String address = addressField.getText();

                // 创建一个 UserInfo 实例
                UserInfo newUser = new UserInfo(userId, userPwd, userName, gender, idCard, phone, address);
                try {
                    addUserByUserInfo(newUser);
                    JOptionPane.showMessageDialog(null, "用户添加成功！", "成功", JOptionPane.INFORMATION_MESSAGE);

                    // 添加成功后，通知主窗口刷新表格数据
                    refreshTableCallback.run();
                } catch (SQLException ex) {
                    JOptionPane.showMessageDialog(null, "添加用户时发生错误：" + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                    throw new RuntimeException(ex);
                } finally {
                    // 可能需要在此处关闭 AddUserWindow 窗口
                    dispose();
                }
//                    System.out.println("用户ID: " + newUser.getUserID()   + ", 用户密码: " + newUser.getUserPWD() + ", 用户名: " + newUser.getUserName() + ", 用户性别: " + newUser.getUserSex() + ", 身份证号: " + newUser.getIDCard()+ ", 电话号码: " + newUser.getPhone() + ", 地址: " + newUser.getAddress());

                // 将新用户添加到 UserManager 的列表中
//                UserManager.addUser(newUser);
                // 关闭窗口
                // 清空文本框内容（如果需要的话）
                userIDField.setText("");
                userPWDField.setText("");
                userNameField.setText("");
                userSexComboBox.setSelectedIndex(0);
                idCardField.setText("");
                phoneField.setText("");
                addressField.setText("");
            }
        });


        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("添加数据成功");
                dispose();
            }
        });

        setContentPane(mainPanel);
        setVisible(true);
    }



    interface OnUserAddedListener {
        void onUserAdded();
    }
////    public static void main(String[] args) {
////        EventQueue.invokeLater(() -> {
////            AddUserWindow addUserWindow = new AddUserWindow();
////        });
//    }
}